package p5.code;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class ListSafeTest {


    public static int clientTotal=5000;
    public static int threadTotal=200;

    public static List list= new ArrayList();

    public static Vector vector = new Vector();

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService= Executors.newCachedThreadPool();//线程池
        final Semaphore semaphore=new Semaphore(threadTotal);//信号量
        final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);//阻塞倒计数

        for(int i=0;i<clientTotal;i++){

            executorService.execute(()->{
                try{
                    semaphore.acquire();
                    Mothen();
                    semaphore.release();
                }catch(Exception e){
                    e.printStackTrace();
                }
                countDownLatch.countDown();//计数器减一操作
            });
        }
        countDownLatch.await();//阻塞线程
        executorService.shutdown();//其中先前提交的任务将被执行，但不会接受任何新任务
        System.out.println("ArrayList.length: "+list.size());
        System.out.println("Vector.length: "+vector.size());
    }
    public static void Mothen(){
        list.add(1);
        vector.add(0);
    }

}
